<?php
class CommendAction extends CommonAction {

    //ajax发表评论
    public function post() {
        if(cookie('postlimit')) {
            echo 2;
            exit;
        }
        $newsCommend = M('NewsCommend');
        $condition['nid'] = $this->_post('nid');
        $count = $newsCommend->where($condition)->count();
        $data['user'] = $this->_post('user');
        $data['content'] = $this->_post('content');
        $data['nid'] = $this->_post('nid');
        $data['addtime'] = time();
        $data['floor'] = $count+1;         //楼层
        if($newsCommend->add($data)) {
            setcookie('postlimit', 'limit', time()+60, '/');
            echo 1;
        }
    }

    public function more() {
        C('TOKEN_ON',false);
        if(isset($_GET['id'])) {
            $newsCommend = M('NewsCommend');
            $news = M('News');
            $newsNav = D('PressNav');
            //当前导航
            $cn = $news->where('id='.$_GET['id'])->getField('nav');
            $currentNav = $newsNav->where('id='.$cn)
                                  ->getField('name');


            //分页
            $n = $newsCommend->field('id')->where('nid='.$_GET['id'])->count();                 //评论总数
            parent::page($n, 10);

            //当前新闻的概要
            $current = $news->field(array('id', 'title', 'summary', 'from', 'writer', 'addtime', 'view_count', 'is_commend'))
                            ->where('id='.$_GET['id'])
                            ->find();
            //当前新闻的所有评论
            $allCommend = $newsCommend->field(array('content', 'user', 'addtime', 'floor'))
                                ->where('nid='.$_GET['id'])
                                ->order('addtime DESC')
                                ->limit($this->limit)
                                ->select();

            //同类热评新闻
            $simHot = $news->Table(array('xinxi_news'=>'xn', 'xinxi_news_commend'=>'xc'))
                           ->field(array('xn.id', 'xn.title',
                                         'xn.title'=>'ftitle',
                                         'COUNT(xn.id)'=>'count',
                                         'xn.addtime'
                                   ))
                           ->where('xn.nav='.$cn.' AND xn.id=xc.nid')
                           ->group('xn.id')
                           ->order('count DESC')
                           ->limit(8)
                           ->select();

            $this->assign(array(
                'allcommend'=>$allCommend,
                'current'=>$current,
                'currentnav'=>$currentNav,
                'simhot'=>$simHot,
                'empty'=>'<p style="text-align:center;height:20px;line-height:20px;border-bottom:1px solid #ccc;color:red;">暂时没人评论</p>'
            ));
            $this->display();
        }
    }




}

?>